﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcSiteMapProvider.Extensibility;
using web.data.entities;

namespace web.bussines
{
    public class productMapDynamicNodeProvider : DynamicNodeProviderBase
    {
        public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
        {
            using (var data = new baseEntities())
            {
                List<DynamicNode> list_node = new List<DynamicNode>();
                product_service proSvc=new product_service();
                int? languageId;
                languageId = web.component.global_const.LANGUAGE_ID_DEFAULT;   //default language 1 (vi) if null language
                var list_cate = proSvc.getListCategoryProductBuilder(null, languageId);
                foreach (var item in list_cate)
                {
                    DynamicNode node = new DynamicNode();
                    node.Title = item.category_name;
                    node.Key = "cateId_" + item.category_id;
                    if (item.category_parent != null)
                    {
                        node.ParentKey = "cateId_" + item.category_parent;
                    }
                    node.RouteValues.Add("alias_cate_product", item.category_alias);
                    node.RouteValues.Add("cateId", item.category_id);
                    node.Action = "product";
                    node.Controller = "shop";
                    node.Route = "product_list";

                    list_node.Add(node);

                    var list_product = proSvc.GetListProductByCategoryIdSiteMap(languageId, item.category_id);
                    foreach (var it in list_product)
                    {
                        DynamicNode node_child = new DynamicNode();
                        node_child.Title = it.product_name;
                        node_child.ParentKey = "cateId_" + item.category_id;
                        node_child.RouteValues.Add("alias_cate_product", item.category_alias);
                        node_child.RouteValues.Add("alias_pro", it.product_alias);
                        node_child.RouteValues.Add("id", it.product_id);
                        node_child.Action = "detail";
                        node_child.Controller = "shop";
                        node_child.Route = "product_detail";

                        list_node.Add(node_child);
                    }
                }
                return list_node;
            }           
        }     
    }
}
